package game;

import java.awt.image.BufferedImage;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: liwan
 * @Description:
 * @Date: create in 2021/9/2/002 16:47:26
 */
public class Hero extends Thread {
    private int x;
    private int y;
    private int w;
    private int h;
    private boolean isRight;
    private BufferedImage image;
    private CopyOnWriteArrayList<BufferedImage> rightImage = new CopyOnWriteArrayList<>();
    private CopyOnWriteArrayList<BufferedImage> leftImage = new CopyOnWriteArrayList<>();
    private CopyOnWriteArrayList<BufferedImage> skillImage = new CopyOnWriteArrayList<>();
    private CopyOnWriteArrayList<BufferedImage> attackRightImage = new CopyOnWriteArrayList<>();
    private CopyOnWriteArrayList<BufferedImage> attackLeftImage = new CopyOnWriteArrayList<>();
    private int heroNum;
    private int speed;
    private GamePanel panel;

    public Hero(GamePanel panel) {
        this.heroNum = 8;
        this.panel = panel;
        this.isRight = true;
        this.image = ImageUtil.getImg("../img/h_R0.png");
        for (int i = 0; i < heroNum; i++) {
            this.rightImage.add(ImageUtil.getImg("../img/h_R" + i + ".png"));
            this.leftImage.add(ImageUtil.getImg("../img/h_L" + i + ".png"));
        }
        for (int i = 0; i < 26; i++) {
            this.skillImage.add(ImageUtil.getImg("../img/skill/frame_" + i + ".png"));
        }
        for (int i = 0; i < 6; i++) {
            this.attackRightImage.add(ImageUtil.getImg("../img/attack/a1-R" + i + ".png"));
            this.attackLeftImage.add(ImageUtil.getImg("../img/attack/a1-L" + i + ".png"));
        }
        this.x = 100;
        this.y = 300;
        this.w = this.image.getWidth();
        this.h = this.image.getHeight();
        this.speed = 5;
    }

    /**
     * @Author: liwan
     * @Description: 向右移动
     * @Params:
     * @Return:
     * @DateTime: 2021/9/2 17:01:19
     */
    int rightIndex = 0;

    public void rightMove() {
        this.x += this.speed;
        range();
        rightIndex++;
        rightIndex = rightIndex >= 8 ? 0 : rightIndex;
        this.image = rightImage.get(rightIndex);
        this.panel.repaint();
    }

    /**
     * @Author: liwan
     * @Description: 向上移动
     * @Params:
     * @Return:
     * @DateTime: 2021/9/2 17:06:45
     */
    int topIndex = 0;

    public void topMove() {
        this.y -= this.speed;
        range();
        topIndex++;
        topIndex = topIndex >= 8 ? 0 : topIndex;
        if (this.isRight) {
            this.image = rightImage.get(topIndex);
        } else {
            this.image = leftImage.get(topIndex);
        }
        this.panel.repaint();
    }

    /**
     * @Author: liwan
     * @Description: 向下移动
     * @Params:
     * @Return:
     * @DateTime: 2021/9/2 19:20:48
     */
    int middleIndex = 0;

    public void middleMove() {
        this.y += this.speed;
        range();
        middleIndex++;
        middleIndex = middleIndex >= 8 ? 0 : middleIndex;
        if (this.isRight) {
            this.image = rightImage.get(middleIndex);
        } else {
            this.image = leftImage.get(middleIndex);
        }
        this.panel.repaint();
    }

    /**
     * @Author: liwan
     * @Description: 向左移动
     * @Params:
     * @Return:
     * @DateTime: 2021/9/2 19:20:57
     */
    int leftIndex = 0;

    public void leftMove() {
        this.x -= this.speed;
        range();
        leftIndex++;
        leftIndex = leftIndex >= 8 ? 0 : leftIndex;
        this.image = leftImage.get(leftIndex);
        this.panel.repaint();
    }

    /**
     * @Author: liwan
     * @Description: 停止的时候图片
     * @Params:
     * @Return:
     * @DateTime: 2021/9/2 19:16:04
     */
    public void stopMove() {
        this.w = this.rightImage.get(0).getWidth();
        this.h = this.rightImage.get(0).getHeight();
        if (this.isRight) {
            this.image = this.rightImage.get(0);
        } else {
            this.image = this.leftImage.get(0);
        }
        panel.repaint();
    }

    /**
     * @Author: liwan
     * @Description: 平A
     * @Params:
     * @Return:
     * @DateTime: 2021/9/3 9:50:41
     */
    public void attack() {
        for (int i = 0; i < 6; i++) {
            this.w = image.getWidth();
            this.h = image.getHeight();
            if (isRight) {
                this.image = this.attackRightImage.get(i);
            } else {
                this.image = this.attackLeftImage.get(i);
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            panel.repaint();
        }
    }

    /**
     * @Author: liwan
     * @Description: 英雄技能
     * @Params:
     * @Return:
     * @DateTime: 2021/9/3 8:31:49
     */
    public void skill() {
        for (int i = 0; i < 26; i++) {
            this.image = this.skillImage.get(i);
            this.w = this.image.getWidth();
            this.h = this.image.getHeight();
            this.x = this.x - this.w / 3 + 60;
            this.y = this.y - this.h / 3 + 20;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            panel.repaint();
            this.x = this.x + this.image.getWidth() / 3 - 60;
            this.y = this.y + this.image.getHeight() / 3 - 20;
        }
        this.stopMove();
    }

    /**
     * @Author: liwan
     * @Description: 判断范围
     * @Params:
     * @Return:
     * @DateTime: 2021/9/2 17:04:40
     */
    public void range() {
        int top = 400 - this.h;
        int right = GameFrame.W - this.w;
        int middle = GameFrame.H - this.h - 50;
        this.y = this.y < top ? top : this.y;
        this.y = this.y < middle ? this.y : middle;
        this.x = this.x < right ? this.x : right;
        this.x = this.x < 0 ? 0 : this.x;
    }

    /**
     * @Author: liwan
     * @Description: 判断攻击
     * @Params: enemy
     * @Return:
     * @DateTime: 2021/9/3 13:46:00
     */
    public void isattack(List<Enemy> enemy) {
        for (Enemy enemy1 : enemy) {
            boolean temps = temp(enemy1);
            if (temps) {
                enemy1.setBloodW(enemy1.getBloodW() - 50);
                panel.repaint();
            }
        }
    }

    /**
    * @Author: liwan
    * @Description: 攻击范围
    * @Params: enemy1
    * @Return:
    * @DateTime: 2021/9/3 13:53:54
    */
    private boolean temp(Enemy enemy1) {
        if (enemy1.getX() - 200 <= this.x && enemy1.getX() + 200 > this.x && enemy1.getY() - 100 <= this.y && enemy1.getY() + 100 > this.y) {
            return true;
        }else {
            return false;
        }
    }

    /**
    * @Author: liwan
    * @Description:  技能攻击
    * @Params: enemy
    * @Return:
    * @DateTime: 2021/9/3 14:11:49
    */
    public void isSkill(List<Enemy> enemy) {
        for (Enemy enemy1 : enemy) {
            boolean temps = skillTemp(enemy1);
            if (temps) {
                enemy1.setBloodW(enemy1.getBloodW() - 100);
                panel.repaint();
            }
        }
    }

    /**
    * @Author: liwan
    * @Description: 技能范围
    * @Params: enemy1
    * @Return: {@link boolean}
    * @DateTime: 2021/9/3 14:12:34
    */
    private boolean skillTemp(Enemy enemy1) {
        if (enemy1.getX() - 500 <= this.x && enemy1.getX() + 500 > this.x && enemy1.getY() - 300 <= this.y && enemy1.getY() + 300 > this.y) {
            return true;
        }else {
            return false;
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public boolean isRight() {
        return isRight;
    }

    public void setRight(boolean right) {
        isRight = right;
    }


}
